package com.westvalley.common.action;

import java.io.InputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import weaver.conn.RecordSet;
import weaver.file.ImageFileManager;
import weaver.general.Util;
import weaver.soa.workflow.request.Cell;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.Row;

/***
 * 基础接口
 *
 */
public class BaseAction extends weaver.workflow.action.BaseAction{
	
	protected RecordSet rs;
	
	protected Map<String,String> fieldMap = new HashMap<String, String>();
	
	/**
	 * 获取主表字段的值
	 * @param property 主表Property数组
	 * @param name 字段名
	 * @return value 值
	 */
	protected String getFieldValue(Property[] property, String name){
		for(Property p : property){
			if(Util.null2String(name).equalsIgnoreCase(p.getName()))
				return Util.toExcelData(p.getValue()).trim();
		}
		return "";
	}
	
	/**
	 * 获取主表字段的值
	 * @param property 主表Property数组
	 * @param fieldId 字段ID
	 * @return value 值
	 */
	protected String getFieldValue2(Property[] property, String fieldId){
		fieldId = fieldId.toLowerCase().replaceAll("field", "");
		return getFieldValue(property,Util.null2String(fieldMap.get(fieldId)));
	}
	
	/**
	 * 获取明细表字段的值
	 * @param property 主表Property数组
	 * @param name 字段名
	 * @return value 值
	 */
	protected String getFieldValue(Cell[] cell, String name){
		for(Cell c : cell){
			if(Util.null2String(name).equalsIgnoreCase(c.getName()))
				return Util.toExcelData(c.getValue()).trim();
		}
		return "";
	}
	
	/**
	 * 获取明细表字段的值
	 * @param property 主表Property数组
	 * @param fieldId 字段ID
	 * @return value 值
	 */
	protected String getFieldValue2(Cell[] cell, String fieldId){
		fieldId = fieldId.toLowerCase().replaceAll("field", "");
		return getFieldValue(cell,Util.null2String(fieldMap.get(fieldId)));
	}
	
	/**
	 * 获取明细表字段的值
	 * @param property 主表Property数组
	 * @param name 字段名
	 * @return value 值
	 */
	protected String getFieldValue(Row row, String name){
		Cell[] cell = row.getCell();
		for(Cell c : cell){
			if(Util.null2String(name).equalsIgnoreCase(c.getName()))
				return Util.toExcelData(c.getValue()).trim();
		}
		return "";
	}
	
	/**
	 * 获取明细表字段的值
	 * @param property 主表Property数组
	 * @param fieldId 字段ID
	 * @return value 值
	 */
	protected String getFieldValue2(Row row, String fieldId){
		fieldId = fieldId.toLowerCase().replaceAll("field", "");
		return getFieldValue(row,Util.null2String(fieldMap.get(fieldId)));
	}
	
	/**
	 * 查询流程的信息
	 * @param workflowId 工作流ID
	 * @param requestId 工作流请求ID
	 * @return
	 */
	protected String getMainTable(String workflowId){
		String main_table = "";//流程主表名称
		RecordSet rs = new RecordSet();
		/*****查询流程主表名称*****/
		String sql = "select tablename, detailkeyfield from workflow_bill where id in(select formid from workflow_base where id='"+workflowId+"')";
		rs.executeSql(sql);
		if(rs.next()){
			main_table = Util.null2String(rs.getString("tablename"));
		}
		return main_table;
	}
	
	/**
	 * 查询流程编号
	 * @return
	 */
	protected String getWorkCode(String requestId){
		String code = "";
		RecordSet rs = new RecordSet();
		String sql = " select requestmark from workflow_requestbase where requestid='"+requestId+"' ";
		rs.executeSql(sql);
		if(rs.next()){
			code = Util.null2String(rs.getString("requestmark"));
		}
		return code;
	}
	
	protected void setFieldMap(String workFlowId){
		RecordSet rs = new RecordSet(); 
		String sql = "select w.id,b.id fieldid,b.fieldname,h.labelname,b.viewtype,b.detailtable from workflow_billfield b ";
			   sql +=" left join HtmlLabelInfo h on b.fieldlabel=h.indexid ";
			   sql +=" left join workflow_base w on w.formid=b.billid ";
			   sql +=" where h.languageid='7' and w.id='"+workFlowId+"' order by b.dsporder";
		rs.executeSql(sql);
		while(rs.next()){
			fieldMap.put(Util.null2String(rs.getString("fieldid")), Util.null2String(rs.getString("fieldname")));
		}
	}
	
	
	protected String getSubId(String deptid){
		String id = "";
		RecordSet rs = new RecordSet(); 
		rs.executeSql("select * from HrmDepartment where id="+deptid);
		this.writeLog("---------------------------------SQL:"+"select * from HrmDepartment where id="+deptid);
		if(rs.next()){
			id = Util.null2String(rs.getString("subcompanyid1"));
		}
		return id;
	}
	
	/**
	 * 获取附件对象附件集合
	 * @param docId
	 * @return List(Map{fileName,inputStream})
	 */
	protected List<Map<String,Object>> getDocInfo(String docId){
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		List<Record> records = Db.find("select IMAGEFILEID,IMAGEFILENAME from DOCIMAGEFILE where DOCID=? order by IMAGEFILEID", docId);
		for(Record record : records){
			String imagefileName = record.get("IMAGEFILENAME")+"";
			int imageFileId = record.get("IMAGEFILEID");
			if (imageFileId > 0 && !"".equals(imagefileName)) {
				InputStream inputStream = ImageFileManager.getInputStreamById(imageFileId);
				if(inputStream!=null){
					Map<String,Object> map = new HashMap<String, Object>();
					map.put("fileName", imagefileName);	
					map.put("inputStream", inputStream);	
					list.add(map);
				}
			}
		}
		return list;
	}
	
	/**
	 * 获取附件对象
	 * @param docId
	 * @param index
	 * @return Map{fileName,inputStream}
	 */
	protected Map<String,Object> getDocInfo(String docId,int index){
		List<Map<String,Object>> list = getDocInfo(docId);
		if(list.size()>0){
			return list.get(index);
		}
		return null;
	}
	
	/**
	 * 根据起始日期和承担月份计算截止日期
	 * @param qSRQ
	 * @param fT
	 * @return
	 */
	protected String calTime(String qSRQ, String fT){
        Calendar c = Calendar.getInstance();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = df.parse(qSRQ);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
        c.setTime(date);
        int month = Integer.parseInt(fT);
        c.add(Calendar.MONTH, month - 1);
        String time = df.format(c.getTime());
        return time; 
    }

	
}

